<?php
namespace app\wypadmin\controller;

/**
 * 后台管理
 */
class Index extends Base
{
	protected function _initialize(){
		parent::_initialize();
		$this->cate = Model('Category');
		$this->fang = Model('Fang/fang');
	}

    public function index(){
    	
		$total = $this->fang->count();
		
		$list 		= $this->cate->getList();
		$list 		= $this->menuTree($list);

		$f_countbycate = array();

		foreach ($list as $v) {
			$f_countbycate[] = [
				'catename' => $v['catename'],
				'count' => $this->getCount($v['id']),
				'level' => $v['level'],
				'id' => $v['id']
			];
		}
		$this->assign('total',$total);
		$this->assign('f_countbycate',$f_countbycate);
		$this->assign('webname',$this->webname);
		return $this->fetch();
    }

	public function getCount($cateid){
		$where = [
			'cateid' => $cateid,
		];
		$count = $this->fang->where($where)->count();
		return $count;
	}
/**
 *  递归获取所有分类
 */
    public function menuTree($arr,$pid=0,$level=0){
        static $tree = array();
        foreach ($arr as $v) {
            if ( $v['pid'] == $pid) {
                $v['level'] = $level;
                $v['catename'] = '├ ' . str_repeat('---- ',$level) . $v['catename'];
                $tree[] = $v;
                $this->menuTree($arr,$v['id'],$level+1);
            }
        }
        return $tree;
    }
}
